package com.lipi.mall.seckill.controller;

import com.lipi.common.utils.R;
import com.lipi.mall.seckill.service.SeckillService;
import com.lipi.mall.seckill.to.SecKillSkuRedisTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀活动商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SecKillSkuRedisTO> data = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(data);
    }

    /**
     * 查询${skuId}商品三天内是否有秒杀活动
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R skuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuRedisTO to = seckillService.getSkuSeckillInfo(skuId);

        return R.ok().setData(to);
    }

    /**
     * 秒杀请求
     *
     * @param killId sessionId_skuId
     * @param key    随机码
     * @param num    后买数量
     * @return
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") String num,
                          Model model) {

        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);

        return "success";

    }
}
























